﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Sys_newswriting : System.Web.UI.Page
{
    PM.PMSys.news thenews = new PM.PMSys.news();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tid"] == null || Session["name"] == null || Session["sex"] == null || Session["depart"] == null || Session["usertype"] == null)
        {
            PM.Code.Web.alertmessage.ShowMsg("账号登录已超时，请返回重新登录。", "../index.aspx");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["id"] != null)
            {
                ckbUpdatetime.Visible = true;
                loadnews(int.Parse(Request["id"].ToString().Trim()));
            }
        }
    }
    //编辑模式时载入初始公告数据
    protected void loadnews(int id)
    {
        SqlDataReader reader = thenews.GetDetail(id);
        while(reader.Read())
        {
            ddlNewstype.SelectedIndex = ddlNewstype.Items.IndexOf(ddlNewstype.Items.FindByText(reader[1].ToString().Trim()));
            txtTopic.Text = reader[2].ToString().Trim();
            txtContent.Text = reader[3].ToString();
            lblOldtime.Text = reader[4].ToString();
        }
        reader.Close();
    }

    //编辑模式时更新数据
    protected bool newsupdate(int id)
    {
        if (ckbUpdatetime.Checked)
        {
           return  thenews.Update(ddlNewstype.SelectedValue.ToString(), txtTopic.Text.ToString().Trim(), txtContent.Text.ToString(), DateTime.Now.ToString(), id);
        } 
        else
        {
           return  thenews.Update(ddlNewstype.SelectedValue.ToString(), txtTopic.Text.ToString().Trim(), txtContent.Text.ToString(), lblOldtime.Text.ToString(), id);
        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (ddlNewstype.SelectedValue.ToString().Equals("请选择"))
        {
            PM.Code.Web.alertmessage.ShowMsg("请选择公告类型！");
        }
        else if (String.IsNullOrEmpty(txtTopic.Text.ToString().Trim()))
        {
            PM.Code.Web.alertmessage.ShowMsg("公告标题不可为空！");
        }
        else if (Request["id"] != null)
        {
            if (newsupdate(int.Parse(Request["id"].ToString())))
            {
                PM.Code.Web.alertmessage.ShowMsg("公告编辑成功！", "newsmanager.aspx");
            }
            else
            { 
               PM.Code.Web.alertmessage.ShowMsg("公告编辑失败，请重试。", "newsmanager.aspx");
            }
        }
        else 
        {
            int result;
            result= thenews.Add(ddlNewstype.SelectedValue.ToString(),txtTopic.Text.ToString().Trim(),txtContent.Text.ToString(),DateTime.Now.ToString());
            if (result>0)
            {
                PM.Code.Web.alertmessage.ShowMsg("公告发布成功！", "newsmanager.aspx");
            } 
            else
            {
                PM.Code.Web.alertmessage.ShowMsg("公告发布失败，请重试。");
            }
        }         
    }
}
